﻿
using Microsoft.AspNetCore.Mvc;
using Net6Shell.Dto;


namespace Net6Shell.Controllers
{
    /// <summary>
    /// 上传操作
    /// </summary>
    public class UpLoadController : NoAuthApiControllerBase
    {
        
        public UpLoadController( )
        {
           
        }
 
        /// <summary>
        /// 本地上传图片信息至服务器处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public CommonResult SaveFile()
        {
            CommonResult res = new CommonResult() { ErrMsg = "", Result = false, ReturnData = "" };
            if (Request.Form.Files==null)
            {
                return res;
            }
            foreach (FormFile file in Request.Form.Files)
            {
                #region 文件校验
                string exten = Path.GetExtension(file.FileName).ToLower();
                if (string.IsNullOrEmpty(exten))
                    exten = ".wav";
                //扩展名不符合要求就不处理
                if (exten != ".jpg" && exten != ".png" && exten!=".wav") continue;
                long size = file.Length / 1024; //文件大小KB
                //if (size > 1024)
                //{
                //    res.ErrMsg = "存在过大文件！";
                //    continue;
                //}
                #endregion
                string fileName = DateTime.Now.ToString("yyMMddHHmmss"); // Guid.NewGuid().ToString();       
                string fullpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads"); //校验文件夹是否存在  不存在进行创建
                if (!Directory.Exists(fullpath))
                {
                    Directory.CreateDirectory(fullpath);
                }
                string fullName = Path.Combine(fullpath, fileName + exten);
                using (FileStream stream = new FileStream(fullName, FileMode.Create, FileAccess.Write))
                {
                    file.CopyTo(stream);
                    stream.Flush();
                }

            }
            res.Result = true;
            res.ReturnData = "上传成功";
            return res;
        }

        /// <summary>
        /// 异步上传
        /// </summary>
        /// <param name="fileModel"></param>
        /// <returns></returns>
        [HttpPost]
        public CommonResult UploadAsync([FromForm] object fileModel)
        {
            
            CommonResult res = new CommonResult() { ErrMsg = "", Result = false, ReturnData = "" };
            string fileName = Guid.NewGuid().ToString();
            string filePath = "/uploads/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";//存储路径    一般情况下都是uploads文件夹           
            string fullpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath); //校验文件夹是否存在  不存在进行创建
            if (!Directory.Exists(fullpath))
            {
                Directory.CreateDirectory(fullpath);
            }
            string fullName = fullpath + fileName + ".wav";
            using (FileStream fs = new FileStream(fullName, FileMode.Create))
            {
                //fileModel.File.CopyTo(fs);
                //fs.Flush();
            }

            return res;
        }
        // FileReportDto 对象
        public class FileReportDto
        {
            /// <summary>
            /// 
            /// </summary>
            public string OrderId { get; set; }

            /// <summary>
            /// 报告文件对象
            /// </summary>
            public IFormFile File { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string ReprotVersion { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string BusinessOrigin { get; set; }
        }
    }
}

